﻿import { NgModule } from "@angular/core";
import {
    NavigationEnd,
    RouteConfigLoadEnd,
    RouteConfigLoadStart,
    Router,
    RouterModule,
} from "@angular/router";
import { AppComponent } from "./app.component";
import { AppRouteGuard } from "./shared/common/auth/auth-route-guard";
import { NotificationsComponent } from "./shared/layout/notifications/notifications.component";

@NgModule({
    imports: [
        RouterModule.forChild([
            {
                path: "app",
                component: AppComponent,
                canActivate: [AppRouteGuard],
                canActivateChild: [AppRouteGuard],
                children: [
                    {
                        path: "",
                        children: [
                            {
                                path: "notifications",
                                component: NotificationsComponent,
                            },
                            {
                                path: "",
                                redirectTo: "app/main/dashboard",
                                pathMatch: "full",
                            },
                        ],
                    },
                    {
                        path: "main",
                        loadChildren: "app/main/main.module#MainModule", //Lazy load main module
                        data: { preload: true },
                    },
                    {
                        path: "admin",
                        loadChildren: "app/admin/admin.module#AdminModule", //Lazy load admin module
                        data: { preload: true },
                        canLoad: [AppRouteGuard],
                    },
                    {
                        path: "task",
                        loadChildren: "app/task/task.module#TaskModule", //Lazy load admin module
                        data: { preload: true },
                        canLoad: [AppRouteGuard],
                    },
                    {
                        path: "customer",
                        loadChildren:
                            "app/customer/customer.module#CustomerModule", //Lazy load admin module
                        data: { preload: true },
                        canLoad: [AppRouteGuard],
                    },
                    {
                        path: "duct",
                        loadChildren: "app/duct/duct.module#DuctModule", //Lazy load admin module
                        data: { preload: true },
                        canLoad: [AppRouteGuard],
                    },
                    {
                        path: "**",
                        redirectTo: "notifications",
                    },
                ],
            },
        ]),
    ],
    exports: [RouterModule],
})
export class AppRoutingModule {
    constructor(private router: Router) {
        router.events.subscribe((event) => {
            if (event instanceof RouteConfigLoadStart) {
                abp.ui.setBusy();
            }

            if (event instanceof RouteConfigLoadEnd) {
                abp.ui.clearBusy();
            }

            if (event instanceof NavigationEnd) {
                document
                    .querySelector("meta[property=og\\:url")
                    .setAttribute("content", window.location.href);
            }
        });
    }
}
